﻿HmJS.$Import('log');
HmJS.$Import('core.element.event');
//HmJS.$Import('core.domready');
HmJS.$Import('util.ThreadedLoop');

HmJS.register('demo.module.util.threadloop', function ($ns) {
	var that = {};

	that.todo = function () {
		var buffer = [],
				inspect = false,
				status = document.id('status'),
				ThreadedLoop = $ns.util.ThreadedLoop;

		var create = new ThreadedLoop(5000, {
			'chunk': 100,
			'interval': 3,
			'onStart': function () {
				status.set('text', 'Creating test array...');
				log('started creation...');
			},
			'onStop': function () {
				status.set('text', 'Test array created!');
				log('creation completed...');
			},
			'onProcess': function (i) {
				buffer.include('item ' + (i + 1) + ' at index ' + i);
			}
		});

		document.id('create').addEvent('click', function (e) {
			e.stop();
			create.start();
		});

		document.id('inspect').addEvent('click', function (e) {
			e.stop();

			inspect = new ThreadedLoop(buffer, {
				'onProcess': function (item, i) {
					status.set('text', 'array item: ' + item);
				}
			});

			inspect.start();
			log.info('inspection started...');
		});

		document.id('pause').addEvent('click', function (e) {
			e.stop();
			inspect.pause && inspect.pause();
			log.info('inspection paused...');
		});

		document.id('resume').addEvent('click', function (e) {
			e.stop();
			inspect.resume && inspect.resume();
			log.info('inspection resumed...');
		});
	};

	return that;
});
